<?php

namespace App\Kuafu\CustomerCoupon\Resources;

use App\Http\Resources\BaseResource;
use App\Kuafu\Activity\Enum\ActivityEnum;
use App\Kuafu\Coupon\Enum\CouponEnum;
use App\Kuafu\CouponExt\Enum\CouponExtEnum;
use App\Kuafu\CustomerCoupon\Enum\CodeEnum;
use App\Kuafu\CustomerCoupon\Model\CustomerCouponModel;
use App\Kuafu\CustomerCoupon\Model\CustomerCouponUseHistoryModel;

class CustomCouponUseHistoryListResource extends BaseResource
{

    const STATUS_USED           = 1;
    const STATUS_STAY_EFFECTIVE = 3;
    const STATUS_STAY_FOR_USE   = 0;
    const STATUS_UN_EFFECTIVE   = 4;

    const STATUS_TEXT = [
        self::STATUS_STAY_FOR_USE   => "待使用",
        self::STATUS_USED           => "已使用",
        self::STATUS_STAY_EFFECTIVE => "待生效",
        self::STATUS_UN_EFFECTIVE   => "已失效"
    ];

    public function toArray($request)
    {
        return [
            "id"                     => $this->id,
            "shopId"                 => $this->shopId,
            "customerCouponId"       => $this->customerCouponId,
            "num"                    => 1,
            "operateTime"            => formatDate($this->operateTime),
            "typeText"               => CustomerCouponUseHistoryModel::TYPE_MAP[$this->type] ?? "-",
            "type"                   => $this->type,
            "couponName"             => $this->couponTitle,
            "couponCode"             => $this->couponCode,
            "couponTypeText"         => CouponExtEnum::COUPON_TYPE_TEXT[$this->couponType] ?? "代金券.",
            "status"                 => $this->getStatus(),
            "couponCostPrice"        => (int)$this->couponCostPrice,
            "couponExtDiscountPrice" => (int)$this->couponExtDiscountPrice,
            "createdBy"              => $this->createdBy,
            "orderId"                => (string)$this->orderId,
            "serialNum"              => $this->serialNum,
            "couponFrom"             => $this->couponFrom,
//            "statusText"       => self::STATUS_TEXT[$this->getStatus()],
//            "useStatus"        => $this->type,
//            "useStatus"        => $this->type,
            "canCancel"              =>
                ($this->shopId == request()->header("shopId")
                    && $this->couponFrom == CouponExtEnum::EXT_PLATFORM_KUAFU
                    && $this->type == CustomerCouponUseHistoryModel::TYPE_USE
                )
        ]; // TODO: Change the autogenerated stub
    }


    /**
     * @return int
     */
    public function getStatus(): int
    {
        if ($this->type == CustomerCouponUseHistoryModel::TYPE_USE) {
            $status = self::STATUS_USED;
        } else {
            $status = self::STATUS_UN_EFFECTIVE;
        }

//
//        if ($this->customerCouponRel->status == CodeEnum::COUPON_STATUS_USED) {
//            $status = self::STATUS_USED;
//        } elseif ($this->customerCouponRel->startAt > time()) {
//            $status = self::STATUS_STAY_EFFECTIVE;
//        } elseif ($this->customerCouponRel->startAt < time() && $this->customerCouponRel->endAt > time()) {
//            $status = self::STATUS_STAY_FOR_USE;
//        } else {
//            $status = self::STATUS_UN_EFFECTIVE;
//        }
        return $status;
    }
}
